home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / languages / cleo.lzh / Cleo / source / IO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-24  |  6.6 KB  |  245 lines

  1. /***************************************************************************
  2. *   Ce fichier, ainsi que tous les  modules  l'accompagnant, peut et  doit *
  3. * etre  copié GRATUITEMENT à la seule condition expresse de conserver      *
  4. * l'INTEGRALITE  du  Code Source, de  la documentation, et  des fichiers   *
  5. * annexes du package. Ce logiciel est Shareware, veuilez envoyer 100 FF à  *
  6. * l'auteur pour recevoir regulièrement les nouvelles versions.             *
  7. * Toute modification est INTERDITE sans l'autorisation écrite de l'auteur. *
  8. *            Tous droits réservés à M. DIALLO Barrou, Juillet 1992.        *
  9. ***************************************************************************/
  10.  
  11.           /********* Fonctions d'entrees-Sorties ************/
  12.  
  13. #ifdef msdos
  14.         #include "include\\cleobis.h"
  15.         #include "include\\libs.h"
  16.         #include <io.h>
  17.         #include <sys\\stat.h>
  18.         #include <fcntl.h>
  19. #else
  20.         #include "include/cleobis.h"
  21.         #include "include/libs.h"
  22. #endif
  23.  
  24. #define verbose
  25.  
  26. extern CONST *symb;
  27. extern VAR *var, *curvar;
  28. extern int NbVar;
  29. extern Entete head;
  30. extern PROG *prg;
  31.  
  32. extern char *TxtFileName;
  33. extern char *CodeFileName;
  34. extern char *ErrorFileName;
  35. extern FILE *ErrorF;
  36. extern Erreur Erreurs[];
  37. extern Erreur Avertis[];
  38. extern FILE *CodeF;
  39. extern FILE *TxtF;
  40. extern char *Txt;
  41. extern int *Adress;
  42.  
  43. extern int ExecMode;
  44. extern long AdressSize;
  45. extern long TextSize;
  46. extern long SymbolSize;
  47. extern FCTLIB *extfct, *curextfct, *curlibfct;
  48.  
  49. long Lenfile(FILE *fic)
  50. {
  51.     long ret, pos;
  52.  
  53.     pos= (long) ftell(fic);
  54.     fseek(fic, 0, 2);
  55.     ret= (long) ftell( fic);
  56.     fseek(fic, 0, pos);
  57.     return(ret);
  58. }
  59.  
  60. BOOL ReadConfig( char *filename)
  61. {
  62.     FILE *conf;
  63.  
  64.     char fct[20], val[20];
  65.     BOOL fin=FALSE;
  66.  
  67.     if (filename==(char*)NULL)
  68.         {
  69.          if (!(conf = fopen(CONFIGFILE,"rt")))
  70.             {
  71.             TraitErreur(WARNING, NOCONF,0,0);
  72.             return (FALSE);
  73.             }
  74.         }
  75.     else
  76.         {
  77.             if (!(conf = fopen(filename,"rt")))
  78.                 TraitErreur(FATALERROR, ERRCONF,0,0);
  79.         }
  80.  
  81.     while (!fin)
  82.       {
  83.         fscanf (conf,"%s %s", fct, val);
  84.         if (feof(conf)) fin=TRUE;
  85.  
  86.         if (!strcmp(fct, "SYMBOL"))
  87.             SymbolSize = atoi(val);
  88.         else
  89.         if (!strcmp(fct, "MODE"))
  90.             {
  91.             if (!strcmp(val, "RUN"))
  92.                 ExecMode = RUNNING;
  93.             else if (!strcmp(val,"TRACE"))
  94.                 ExecMode = TRACING;
  95.             else
  96.                 TraitErreur(FATALERROR, MODECONF,0,0);
  97.             }
  98.         else
  99.         if (!strcmp(fct, "ERRORS"))
  100.             {
  101.             ErrorFileName =(char *) strdup(val);
  102.                                 ErrorF = fopen(ErrorFileName,"wt");
  103.             }
  104.         else
  105.         if (!strcmp(fct, "LABEL_MAXLEN"))
  106.             SymbolSize = atoi(val);
  107.         else
  108.             {
  109.             printf("\t'%s' ",fct);
  110.             TraitErreur(FATALERROR, UNKWOWNCONF ,0,0);
  111.             }
  112.     }
  113.         fclose(conf);
  114. }
  115.  
  116. BOOL Ouvre()
  117. {
  118.         int hand=0;
  119. #ifdef msdos
  120.                  if (!(hand = open(TxtFileName,O_RDONLY | O_TEXT, S_IREAD)))
  121.         {
  122.                                 TraitErreur(RECERROR,FICTXT,0,0);
  123.                                 return (FALSE);
  124.                   }
  125.                   TextSize = filelength(hand);
  126.                   TxtF = fdopen (hand, "rt");
  127.                   rewind(TxtF);
  128. #else
  129.         if (!(TxtF = fopen(TxtFileName,"rt")))
  130.         {
  131.                                 TraitErreur(RECERROR,FICTXT,0,0);
  132.             return (FALSE);
  133.         }
  134.                  TextSize=Lenfile(TxtF);
  135. #endif
  136.         if (!(CodeF = fopen(CodeFileName,"wb")))
  137.         {
  138.             TraitErreur(RECERROR,ERRCODE,0,0);
  139.             return (FALSE);
  140.                   }
  141.  
  142.          if (!(Txt = (char*)calloc((int)TextSize+1,1)))
  143.                   TraitErreur(FATALERROR,MEMTXT,0,0);
  144.          fread(Txt,(int)TextSize,1,TxtF);          /* Lecture du Source */
  145.     if (TxtF != NULL)   fclose(TxtF);
  146.     return (TRUE);
  147. }
  148.  
  149. void WriteCode(void)
  150. {
  151.         CONST *cur=symb;       /* Pointeur sur les constantes */
  152.         PROG *prog = prg;      /* Pointeur sur le prog. */
  153.         VAR *curv= var;        /* Pointeur sur les variables */
  154.         int tname;
  155.         FCTLIB *curl;
  156.  
  157.        fwrite(&head, sizeof(head),1, CodeF);     /* Ecris l'entete */
  158.        fprintf(CodeF, SECTDATA);
  159. #ifdef verbose
  160.     printf("Writing Code...");
  161. #endif
  162.  
  163.         if (head.Char)
  164.         {
  165.         cur = symb;
  166.         while (cur !=NULL)
  167.          {
  168.          if (cur->type== constchr_mt)
  169.            fwrite( &cur->variable.Char,sizeof(char),1, CodeF);   /* Ecris Constantes Char */
  170.          cur = cur->next;
  171.          }
  172.         }
  173.  
  174.         if (head.integer)
  175.         {
  176.         cur = symb;
  177.         while (cur !=NULL)
  178.          {
  179.          if (cur->type== constint_mt)
  180.             fwrite( &cur->variable.integer,sizeof(long),1, CodeF);   /* Ecris Constantes Integer */
  181.          cur = cur->next;
  182.          }
  183.         }
  184.  
  185.         if (head.real)
  186.         {
  187.         cur = symb;
  188.         while (cur !=NULL)
  189.          {
  190.          if (cur->type== constreal_mt)
  191.             fwrite( &cur->variable.real,sizeof(double),1, CodeF);   /* Ecris Constantes Real */
  192.          cur = cur->next;
  193.          }
  194.         }
  195.  
  196.         if (head.string)
  197.         {
  198.         cur = symb;
  199.         while (cur !=NULL)
  200.          {
  201.          if (cur->type== conststr_mt)
  202.             fwrite( &cur->variable.string, MAXSTRING ,1, CodeF);   /* Ecris Constantes String */
  203.          cur = cur->next;
  204.          }
  205.         }
  206.  
  207.         if (head.Varray)     /* ecris les tableaux: borne1, borne2, type */
  208.         {
  209.         curv = var;
  210.         while (curv !=NULL)
  211.          {
  212.          if (curv->type == array_t)
  213.              fwrite( &curv->tab, sizeof(int)*3 ,1, CodeF);
  214.          curv = curv->next;
  215.          }
  216.         }
  217.  
  218.         fprintf(CodeF, SECTLIB);
  219.  
  220.         if (head.nbrefctlib)
  221.         {
  222.             curl = extfct;
  223.             while (curl)
  224.             {
  225.                 fwrite(&curl->node, sizeof(int),1, CodeF);        /* node */
  226.                 fwrite(&curl->id, sizeof(int),1, CodeF);          /* id */
  227.                 fwrite(&curl->retype, sizeof(int),1, CodeF);      /* typeret */
  228.                 fwrite(&curl->nbarg,sizeof(int),1, CodeF);        /* Nbre d'args */
  229.                 fwrite(curl->type,sizeof(int)*curl->nbarg ,1, CodeF); /*tab de type */
  230.                 curl = curl->next;
  231.             }
  232.         }
  233.         fprintf(CodeF, SECTCODE);
  234.  
  235.         while (prog != NULL)
  236.         {
  237.           if ( prog->code == BRA || prog->code == BNE)
  238.               prog->operande = Adress[prog->operande];
  239.  
  240.           fwrite (&prog->code, sizeof(int),1, CodeF);
  241.           fwrite (&prog->operande, sizeof(int),1, CodeF);
  242.           prog = prog->next;
  243.         }
  244. }
  245.